package hijava;

public class Storage14 {

    int count = 0;
    int max = 20;
    public synchronized void add(){
        while (count>=max){
            System.out.println(Thread.currentThread().getName()+":仓库已满");
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        count++;
        System.out.println(Thread.currentThread().getName()+":【生产】了1个,当前的仓库数量是:"+ count);
        this.notifyAll();
    }

    public synchronized void subtract(){
        while (count<=0){
            System.out.println(Thread.currentThread().getName()+":仓库-空");
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        count--;
        System.out.println(Thread.currentThread().getName()+":【消费】了1个,当前的仓库数量是:"+ count);
        this.notifyAll();
    }

}
